package com.qiumo.gmtool.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.qiumo.gmtool.common.RetResult;
import com.qiumo.gmtool.config.GenshinConfig;
import com.qiumo.gmtool.mail.MailUtil;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * gm角色功能
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@SaCheckPermission("avatar")
@RequestMapping("/gm/avatar")
public class AvatarController {

    private final MailUtil mailUtil;

    private final GenshinConfig genshinConfig;

    /**
     * 获取角色列表
     */
    @GetMapping("/getAllAvatar")
    public RetResult getAllAvatar() {
        try {
            Path filePath = Paths.get(genshinConfig.keyPath, "gm", "Avatar.txt");
            List<Map<String, Object>> list;
            try (Stream<String> lines = Files.lines(filePath)) {
                list = lines.map(line -> {
                    String[] split = line.split(":");
                    Map<String, Object> map = new HashMap<>();
                    map.put("id", split[0]);
                    map.put("name", split[1]);
                    return map;
                }).collect(Collectors.toList());
            }
            return RetResult.succ(list);
        } catch (IOException e) {
            log.error("Failed to read Avatar.txt file", e);
            return RetResult.err("获取角色列表失败");
        }
    }


    /**
     * 添加或者删除角色
     */
    @GetMapping("/addOrDelAvatar/{type}")
    public RetResult addOrDelAvatar(@PathVariable("type") String type, String id) {
        String zl = "";
        if (type.equals("1")) {
            zl = "avatar del " + id;
        } else {
            zl = "avatar add " + id;
        }
        return mailUtil.fastSend(zl);
    }

    /**
     * 设置技能等级
     */
    @GetMapping("/setLevel/{type}")
    public RetResult setLevel(@PathVariable("type") String type, String level) {
        if (type.equals("1") || type.equals("2") || type.equals("3") || type.equals("all")) {
            String zl = "skill " + type + " " + level;
            return mailUtil.fastSend(zl);
        } else {
            return RetResult.err("无效技能类型");
        }
    }

    /**
     * 设置其他等级 type[1：冒险等级 2：角色突破等级 3：角色等级]
     */
    @GetMapping("/setOtherLevel/{type}")
    public RetResult setOtherLevel(@PathVariable("type") String type, String level) {
        String zl;
        switch (type) {
            case "1":
                zl = "player level " + level;
                break;
            case "2":
                zl = "break " + level;
                break;
            case "3":
                zl = "level " + level;
                break;
            default:
                return RetResult.err("出问题辣~");
        }
        return mailUtil.fastSend(zl);
    }
}
